﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Web.Paper;
using PowerCreator.LMS.Web.Question;
using PowerCreator.LMS.Paper.Entiry;
using PowerCreator.LMS.Question.Entiry;
using PowerCreator.LMS.ViewQuery;
using PowerCreator.LMS.Common.Script;



public partial class sysadmin_PaperManager_Course_ClassUser : PowerCreator.LMS.Web.Base.BasePage
{
    ManagerPaper objManagerPaper = new ManagerPaper();
    ManagerQuestion objManagerQuestion = new ManagerQuestion();
    ViewQuery objViewQuery = new ViewQuery();

    const string SEARCH_USERPAPERHISTROY_PAPER_TUSER_TTRAINCLASS = "select distinct * from (select UserPaperHistory.UPHistoryID ,UserPaperHistory.UPHistorySubmitTime,UserPaperHistory.FKUPHistoryCourseID,UserPaperHistory.UPHistoryReadTime,UserPaperHistory.UPHistoryReadState,UserPaperHistory.UPHistoryScore,Paper.PaperName,TO_Learner.UserName,TO_Learner.Name,UserPaperHistory.FKUPHistoryClassID,UserPaperHistory.FKUPHistoryUserID,UserPaperHistory.FKUPHistoryProjectID,UserPaperHistory.FKUPHistoryPaperID, TO_Class.Name as ClassName from UserPaperHistory,Paper ,TO_Learner, TO_Class where Paper.PaperID=UserPaperHistory.FKUPHistoryPaperID and UserPaperHistory.FKUPHistoryUserID=TO_Learner.Learner_Id and UserPaperHistory.FKUPHistoryClassID=TO_Class.Class_Id and UserPaperHistory.FKUPHistoryCourseID={0}) as tb where 1=1 {1}";
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PageLoad();
            //ViewState["ProjectID"] =Request.QueryString["p"].ToString();
            ViewState["CourseID"] = Request.QueryString["CID"].ToString();
            string search = GetSearch();
            BinderPager(search);
            BinderGV(search, 1);
            InitPage();
            //GridBand("", -1, -1, int.Parse(ViewState["ProjectID"].ToString()));
            
        }
    }

    protected override void InitPage()
    {
        lab_ReadState.Text = GetLanageValueByKey("lab_PaperUserList_ReadState", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Username.Text = GetLanageValueByKey("lab_PaperUserList_UserName", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }


    private string GetSearch()
    {
        string username = txtUserName.Text.Trim();
        int readstate = int.Parse(dpdlst_ReadState.SelectedValue);
        StringBuilder search = new StringBuilder();
        if (!string.IsNullOrEmpty(username))
        {
            search.Append(" and (tb.UserName like '%" + username + "%' or tb.Name like '%"+username+"%')");
        }
        if (readstate != -1)
        {
            search.Append(" and tb.UPHistoryReadState='" + readstate + "'");
        }
        string strSearch = string.Format(SEARCH_USERPAPERHISTROY_PAPER_TUSER_TTRAINCLASS, ViewState["CourseID"].ToString(), search);
        return strSearch;
    }
    private void BinderPager(string search)
    {
        AspNetPager1.RecordCount = objViewQuery.Count(search);
        AspNetPager1.PageSize = LMSPageSize;
    }

    private void BinderGV(string search, int pageIndex)
    {
        DataTable dt = new DataTable();
        dt = objViewQuery.Query(search, LMSPageSize, pageIndex);
        dt.Columns.Add("url");
        //dt.Columns.Add("ClassExam_ID");
        //dt.Columns.Add("CourseName");

        PowerCreator.LMS.Web.DL.Project.TOClassExam objClassExam = new PowerCreator.LMS.Web.DL.Project.TOClassExam();
        PowerCreator.LMS.Web.Course.Course objCourse = new PowerCreator.LMS.Web.Course.Course();
        PowerCreator.LMS.Course.Entity.VOTOCourse vocourse = new PowerCreator.LMS.Course.Entity.VOTOCourse();
        
        
        

        for (int i = 0; i < dt.Rows.Count; i++)
        {

            #region 获取班级试卷表中的id

            //if (ViewState["ClassID"] != null)
            //{
            //    string search_classexam = " ClassID=" + ViewState["ClassID"].ToString().Trim() + " and Exam_ID=" + dt.Rows[i]["FKUPHistoryPaperID"].ToString().Trim() + "";
            //    DataTable dt_classexam = objClassExam.Query(search_classexam);
            //    if (dt_classexam.Rows.Count == 0)
            //    {
            //        dt.Rows[i]["ClassExam_ID"] = 0;
            //        dt.Rows[i]["CourseName"] = "";
            //    }
            //    else
            //    {
            //        dt.Rows[i]["ClassExam_ID"] = dt_classexam.Rows[0]["ClassExam_ID"].ToString();
            //        if (dt_classexam.Rows[0]["Course_ID"].ToString() == "0")
            //            dt.Rows[i]["CourseName"] = "综合";
            //        else
            //        {
            //            vocourse = objCourse.LoadVOTCourse(int.Parse(dt_classexam.Rows[0]["Course_ID"].ToString()));
            //            dt.Rows[i]["CourseName"] = vocourse.CourseName;
            //        }
            //    }

            //}
            //if (dt.Rows[i]["FKUPHistoryCourseID"].ToString().Trim() == "0")//0:试卷，1：课程底下的试卷
            //{
            //    string search_ProjectResource = " ProresClass=2 and FKProresTrainproID=" + dt.Rows[i]["FKUPHistoryProjectID"].ToString() + " and FKProresCourseID=" + dt.Rows[i]["FKUPHistoryCourseID"].ToString() + " and FKProresPaperID=" + dt.Rows[i]["FKUPHistoryPaperID"].ToString();
            //    DataTable dt_ProjectResource = objProjectResource.QueryProjectResource(search_ProjectResource);
            //    if (dt_ProjectResource.Rows.Count == 0)
            //    {
            //        dt.Rows[i]["ProjectResourceID"] = 0;
            //    }
            //    else
            //    {
            //        dt.Rows[i]["ProjectResourceID"] = dt_ProjectResource.Rows[0]["ProresID"].ToString();
            //    }
            //    dt.Rows[i]["CourseResourceID"] = "0";
            //}
            //else
            //{
            //    string search_ProjectResource = " ProresClass=1 and FKProresTrainproID=" + dt.Rows[i]["FKUPHistoryProjectID"].ToString() + " and FKProresCourseID=" + dt.Rows[i]["FKUPHistoryCourseID"].ToString() + " and FKProresPaperID=" + dt.Rows[i]["FKUPHistoryPaperID"].ToString();
            //    DataTable dt_ProjectResource = objProjectResource.QueryProjectResource(search_ProjectResource);

            //    if (dt_ProjectResource.Rows.Count == 0)
            //    {
            //        dt.Rows[i]["ProjectResourceID"] = 0;
            //    }
            //    else
            //    {
            //        dt.Rows[i]["ProjectResourceID"] = dt_ProjectResource.Rows[0]["ProresID"].ToString();
            //    }

            //    string search_CourseResource = " fkCoursersResourceID=0 and CoursersClass=2 and FKCourseresCourseID=" + dt.Rows[i]["FKUPHistoryCourseID"].ToString() + " and FKCoursersPaperID=" + dt.Rows[i]["FKUPHistoryPaperID"].ToString();

            //    DataTable dt_CourseResource = objCourse.QueryCourseResource(search_CourseResource);

            //    if (dt_CourseResource.Rows.Count == 0)
            //    {
            //        dt.Rows[i]["CourseResourceID"] = 0;
            //    }
            //    else
            //    {
            //        //dt.Rows[i]["CourseResourceID"] = dt_ProjectResource.Rows[0]["CourseresID"].ToString();
            //        dt.Rows[i]["CourseResourceID"] = dt_CourseResource.Rows[0]["CourseresID"].ToString();      //Skey 更改 2010-4-22 1037
            //    }

            //}
            #endregion

            dt.Rows[i]["url"] = "PaperID=" + dt.Rows[i]["FKUPHistoryPaperID"].ToString() + "&ProjectID=" + dt.Rows[i]["FKUPHistoryProjectID"].ToString() + "&ClassID=" + dt.Rows[i]["FKUPHistoryClassID"].ToString() + "&UserID=" + dt.Rows[i]["FKUPHistoryUserID"].ToString() + "&CourseID=" + dt.Rows[i]["FKUPHistoryCourseID"].ToString() + "&ProjectResourceID=0&CourseResourceID=0";
        }
        gvPaperUserList.DataSource = dt;
        gvPaperUserList.DataBind();

        PreAspNetPager(AspNetPager1);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        //搜索
        string search = GetSearch();
        BinderPager(search);
        BinderGV(search, 1);

    }
    //private void GridBand(string username, int classID, int readstate, int projectid)
    //{
    //    DataTable dt = new DataTable();
    //    StringBuilder str = new StringBuilder();
    //    str.Append("select * from (select UserPaperHistoryInfo.UPHID as ID,UserPaperHistoryInfo.SubmitDate,UserPaperHistoryInfo.ReadState,UserPaperHistoryInfo.PaperScore,PaperInfo.PaperName,SecurityUserInfo.UserName,ClassInfo.ClassName,UserPaperHistoryInfo.ClassID,UserPaperHistoryInfo.UserID,UserPaperHistoryInfo.ProjectID,UserPaperHistoryInfo.PaperID from UserPaperHistoryInfo,PaperInfo ,SecurityUserInfo,ClassInfo where  PaperInfo.PaperID=UserPaperHistoryInfo.PaperID and UserPaperHistoryInfo.UserID=SecurityUserInfo.UserID and ClassInfo.ClassID=UserPaperHistoryInfo.ClassID and UserPaperHistoryInfo.ProjectID='" + projectid + "') as tb where 1=1 ");
        
    //    if (classID != -1)
    //    {
    //        str.Append(" and  tb.ClassID='" + classID + "'");
    //    }
    //    if (!string.IsNullOrEmpty(username))
    //    {
    //        str.Append(" and tb.UserName like '%" + username + "%'");
    //    }
    //    if (readstate != -1)
    //    {
    //        str.Append(" and ReadState='" + readstate + "'");
    //    }

    //    ViewState["queryString"] = str.ToString();
    //    dt = FPager.ExecutePager(Convert.ToInt32(ViewState["pageIndex"].ToString()), Convert.ToInt32(ViewState["pageSize"].ToString()), ViewState["showString"].ToString(), ViewState["queryString"].ToString(), ViewState["whereString"].ToString(), ViewState["orderString"].ToString(), ViewState["tabName"].ToString(), out pageCount, out recordCount, out dqpageCount);
    //    dt.Columns.Add("url");
    //    for (int i = 0; i < dt.Rows.Count; i++)
    //    {
    //        dt.Rows[i]["url"] = "UserID=" + dt.Rows[i]["UserID"].ToString() + "&ProjectID=" + dt.Rows[i]["ProjectID"].ToString() + "&ClassID=" + dt.Rows[i]["ClassID"].ToString() + "&PaperID=" + dt.Rows[i]["PaperID"].ToString();

    //    }

    //    gvPaperUserList.DataSource = dt;
    //    ViewState["pageCount"] = pageCount;
    //    ViewState["recordCount"] = recordCount;
    //    this.labRecordCount.Text = recordCount.ToString();
    //    this.labPageCount.Text = pageCount.ToString();
    //    this.lab_dqpageCount.Text = this.dqpageCount.ToString();
    //    gvPaperUserList.DataBind();
    //    check();

    //}
    private void PageLoad()
    {
        PowerCreator.LMS.Web.Project.ProjectUser obj = new PowerCreator.LMS.Web.Project.ProjectUser();


        dpdlst_ReadState.Items[0].Text = GetLanageValueByKey("Msg_ManagerQuestion_PleaseCheck", PowerCreator.LMS.Language.LanagetItemType.Page);
        dpdlst_ReadState.Items[1].Text = GetLanageValueByKey("msg_PaperUserList_weipiyue", PowerCreator.LMS.Language.LanagetItemType.Page);
        dpdlst_ReadState.Items[2].Text = GetLanageValueByKey("msg_PaperUserList_yipiyue", PowerCreator.LMS.Language.LanagetItemType.Page);
        
    }
    protected void gvPaperUserList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string url = string.Empty;
        if (e.CommandName == "readpaper")
        {
            url = "ReadPaper.aspx?" + e.CommandArgument.ToString();

        }
        else if (e.CommandName == "studyhis")
        {
             url = "xxaspx?" + e.CommandArgument.ToString();
        }
        else
        {
            url = "EditUserPaper.aspx?" + e.CommandArgument.ToString();
           
        }
        Script.RedirectURL(url);
    }
    protected void gvPaperUserList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.Header)
        //{
            //e.Row.Cells[0].Text = GetLanageValueByKey("lab_PaperUserList_UserName", PowerCreator.LMS.Language.LanagetItemType.Page);
            //e.Row.Cells[1].Text = GetLanageValueByKey("lbl_AddClass_TrainclaName", PowerCreator.LMS.Language.LanagetItemType.Page);

            //e.Row.Cells[2].Text = GetLanageValueByKey("lab_EditUserPaper_PaperName", PowerCreator.LMS.Language.LanagetItemType.Page);
            //e.Row.Cells[3].Text = GetLanageValueByKey("msg_ProjectClassUser_SubmitTime", PowerCreator.LMS.Language.LanagetItemType.Page);
            //e.Row.Cells[4].Text = GetLanageValueByKey("lab_ManagerPaperInfo_PaperState", PowerCreator.LMS.Language.LanagetItemType.Page);
            //e.Row.Cells[5].Text = GetLanageValueByKey("lab_EditUserPaper_RealScore", PowerCreator.LMS.Language.LanagetItemType.Page);
            //e.Row.Cells[6].Text = GetLanageValueByKey("msg_PaperUserList_pingjuanshijian", PowerCreator.LMS.Language.LanagetItemType.Page);
            //e.Row.Cells[7].Text = GetLanageValueByKey("btn_ManagerQuestion_Work", PowerCreator.LMS.Language.LanagetItemType.Page);

            //e.Row.Cells[8].Text = GetLanageValueByKey("msg_PaperUserList_pingjuanren", PowerCreator.LMS.Language.LanagetItemType.Page);
            //e.Row.Cells[9].Text = GetLanageValueByKey("btn_ManagerQuestion_Work", PowerCreator.LMS.Language.LanagetItemType.Page);

        //}
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("bgColor", "#F7FAFF");
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#FAEDB8'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#F7FAFF'");

            LinkButton btn = new LinkButton();
            btn = (LinkButton)e.Row.Cells[6].FindControl("btn_readpaper");
            LinkButton btn_Edit = (LinkButton)e.Row.Cells[6].FindControl("btn_Edit");
            btn.Text = GetLanageValueByKey("btn_ProjectClassUser_ReadPaper", PowerCreator.LMS.Language.LanagetItemType.Page);
            btn_Edit.Text = GetLanageValueByKey("btn_ProjectClassUser_UpdatePaper", PowerCreator.LMS.Language.LanagetItemType.Page);

            if (e.Row.Cells[4].Text.Trim() == "0")
            {
                e.Row.Cells[4].Text = GetLanageValueByKey("msg_PaperUserList_weipiyue", PowerCreator.LMS.Language.LanagetItemType.Page);
                btn_Edit.Enabled = false;
                btn.Enabled = true;
            }
            else
            {
                e.Row.Cells[4].Text = GetLanageValueByKey("msg_PaperUserList_yipiyue", PowerCreator.LMS.Language.LanagetItemType.Page);
                btn_Edit.Enabled = true;
                btn.Enabled = false;
            }
            if (e.Row.Cells[3].Text.Trim() == "&nbsp;")
            {
                e.Row.Cells[3].Text = GetLanageValueByKey("msg_ProjectClassUser_NoAccomplish", PowerCreator.LMS.Language.LanagetItemType.Page);
                btn.Enabled = false;
            }

        } 


    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderGV(search, AspNetPager1.CurrentPageIndex);
    }
}
